package org.smile.auth.session.redis;

import org.smile.auth.session.AbstractManager;
import org.smile.auth.session.Session;
import org.smile.util.StringUtils;

import java.io.IOException;

public class RedisManager extends AbstractManager {

    @Override
    public Session createSession(String sessionId) {
        RedisSession session=new RedisSession(this);
        session.setStore(this.store);
        session.setCreationTime(System.currentTimeMillis());
        if(StringUtils.isBlank(sessionId)){
            sessionId=this.idGenerator.generate();
        }
        session.setId(sessionId);
        add(session);
        this.store.saveBase(session);
        return session;
    }

    @Override
    public Session findSession(String id)  {
        Session session=super.findSession(id);
        if(session!=null){
            return session;
        }
        session=this.store.load(id);
        return session;
    }

    @Override
    public Session findSessionByToken(String token) {
        return this.store.loadByToken(token);
    }
}
